<?php
class buecher extends GetData{

	private $daten;
	public $System;

	public function __construct($match){
		$this->daten = $match;
	}

	public function getWort(RainTPL $design){
		$filename =	$_SERVER['DOCUMENT_ROOT']."/import/buecher.csv";
		$handle = fopen ($filename,"r");
		while ( ($data = fgetcsv ($handle, 10000, ";")) !== FALSE ) {
			$csv_autor = null;
			$csv_publisher = null;
			$csv_pic = null;
			$csv_title = null;
			$csv_relase = '0000-00-00';
			$csv_bewertung = 0;
			$csv_groesse = null;
			$csv_art = null;
			$csv_isbn10 = null;
			$csv_isbn13 = null;
			$csv_preis = 0;
			$csv_sprache = null;
			$csv_original_title = null;
			$csv_subcategory = null;
			$csv_tag = null;
			$csv_pagesize = null;
			$csv_bewertung = null;
			
			if($data[1]){
				$csv_title = $data[1];
			}
			if(isset($data[7])){
				$csv_autor = $data[7];
			}
			if(isset($data[2])){
				$csv_publisher = $data[2];
			}
			if(isset($data[10])){
				$csv_pic = $data[10];
			}		
			if(isset($data[6])){
				$tmp = explode(" ", $data[6]);
				$csv_bewertung = $tmp[0];
			}	
			if(isset($data[5])){
				$csv_isbn10 = $data[5];
			}
			if(isset($data[4])){
				$csv_isbn13 = str_replace("_", "", $data[4]);
			}
			if(isset($data[11])){
				$csv_pagesize = $data[11];
			}
			if(isset($data[8])){
				$csv_art = $data[8];
			}
			if(isset($data[3])){
				$csv_groesse = $data[3];
			}
			if(isset($data[9])){
				$csv_preis = str_replace(",", ".", $data[9]);
			}		
						
			$ObAutor = new Db_Autor();
			$autor = $ObAutor->getAutorByName($csv_autor);
			
			if(!$autor)
			{
				$ObAutor->setAutor($csv_autor);
				$autorid = $ObAutor->getLastInsertId();
			}
			
			if(is_array($autor) && $autor)
				$autorid = $autor[0]->id;
				
				
			$ObPublisher = new Db_Publisher();
			$publisher = $ObPublisher->getPublisherByName($csv_publisher);
			
			if(!$publisher)
			{
				$ObPublisher->setPublisher($csv_publisher);
				$publisherid = $ObPublisher->getLastInsertId();
			}
			
			if(is_array($publisher) && $publisher){
				$publisherid = $publisher[0]->id;
			}
			
			$ObBook = new Db_Book();
			$DBbook = $ObBook->getBookByName($csv_title);
			
			if($DBbook)
			{
				$change = false;
				
				if (count($DBbook[0]->autor) <= 0 and $autorid){
					$change = $ObBook->updateBookWithTuppel("autor", $autorid);
				}
				if (count($DBbook[0]->publisher) <= 0 and $publisherid){
					$change = $ObBook->updateBookWithTuppel("publisher", $publisherid);
				}
				if (!$DBbook[0]->isbn13 and $csv_isbn13 != ''){
					$change = $ObBook->updateBookWithTuppel("isbn13", $csv_isbn13);
				}
				if (!$DBbook[0]->isbn10 and $csv_isbn10 != ''){
					$change = $ObBook->updateBookWithTuppel("isbn10", $csv_isbn10);
				}			
				if (!$DBbook[0]->pic and $csv_pic != ''){
					$change = $ObBook->updateBookWithTuppel("pic", $csv_pic);
				}
				if (!$DBbook[0]->bewertung = 0 and $csv_bewertung > 0){
					$change = $ObBook->updateBookWithTuppel("bewertung", $csv_bewertung);
				}
				if (!$DBbook[0]->groesse and $csv_groesse != ''){
					$change = $ObBook->updateBookWithTuppel("groesse", $csv_groesse);
				}
				if (!$DBbook[0]->art and $csv_art != ''){
					$change = $ObBook->updateBookWithTuppel("art", $csv_groesse);
				}
				if (!$DBbook[0]->preis and $csv_preis > 0){
					$change = $ObBook->updateBookWithTuppel("preis", $csv_preis);
				}
				if (!$DBbook[0]->sprache and $csv_sprache != ''){
					$change = $ObBook->updateBookWithTuppel("sprache", $csv_sprache);
				}
				if (!$DBbook[0]->original_title and $csv_original_title != ''){
					$change = $ObBook->updateBookWithTuppel("original_title", $csv_original_title);
				}
				if (!$DBbook[0]->subcategory and $csv_subcategory > 0){
					$change = $ObBook->updateBookWithTuppel("subcategory", $csv_subcategory);
				}
				if (!$DBbook[0]->tag and $csv_tag != ''){
					$change = $ObBook->updateBookWithTuppel("tag", $csv_tag);
				}
				if (!$DBbook[0]->pagenumbers and $csv_pagesize > 0){
					$change = $ObBook->updateBookWithTuppel("pagenumbers", $csv_pagesize);
				}
				
				if($change){
					$ObBook->dbUpdateBook($DBbook[0]->id);
					echo "UPDATE ".$DBbook[0]->title."<br />";
				}
				
			}
			else
			{					
				$book = new Db_Book($csv_title, $autorid, 1, $publisherid, $csv_relase,$csv_original_title,$csv_subcategory,$csv_isbn13,$csv_isbn10,$csv_pagesize,$csv_tag,$csv_pic,$csv_sprache,$csv_preis,$csv_art,$csv_groesse,$csv_bewertung);
				$book->setBook();
				
				echo "INSERT ".$csv_title."<br />";
			}
			
			
		}
		fclose ($handle);
	}

}
?>